<!--#include file="config.asp"-->
<%
''' SDCMS 支付宝登录绑定
''' ==================================================================
''' 版权所有 http://www.sdcms.cn
''' ------------------------------------------------------------------
''' 这不是一个自由软件！您只能在不用于商业目的的前提下对程序代码进行修改和使用；
''' 未经授权不允许对程序代码以任何形式任何目的的再发布。
''' ==================================================================
''' 编写: IT平民
''' 修改：IT平民 in 2012.0

	dim alipay_user_id:alipay_user_id=sdcms.loadsession("alipay_user_id")
	dim api_backurl:api_backurl=sdcms.fget("api_backurl",0)
	dim nickname:nickname=sdcms.enhtml(sdcms.fget("nickname",0))
	
	function check_user_info()
		dim t0:t0=false
		if sdcms.strlen(alipay_user_id)=0 then
			t0=true
		end if
		check_user_info=t0
	end function
	
	sub get_user_info()
		if check_user_info then
			sdcms.echo "&#20250;&#35805;&#36807;&#26399;&#65292;&#26080;&#27861;&#30331;&#24405;"
			sdcms.die
			exit sub
		end if
	end sub
	
	sub checkbind()
		if check_user_info then
			sdcms.echo "0&#20250;&#35805;&#36807;&#26399;&#65292;&#26080;&#27861;&#32465;&#23450;"
			exit sub
		end if
		
		dim t0,t1,t2,data,arr
		t0=sdcms.enhtml(sdcms.fpost("username",0))
		t1=sdcms.enhtml(sdcms.fpost("password",0))
		if sdcms.strlen(t0)=0 then
			sdcms.ajaxjson "&#36134;&#25143;&#19981;&#33021;&#20026;&#31354;",0
			exit sub
		end if
		if not(sdcms.checkstr(t0,"username")) then
			sdcms.ajaxjson "&#36134;&#25143;&#19981;&#31526;&#21512;&#35201;&#27714;",0
			exit sub
		end if
		if sdcms.strlen(t1)=0 then
			sdcms.ajaxjson "&#23494;&#30721;&#19981;&#33021;&#20026;&#31354;",0
			exit sub
		end if
		if not(sdcms.checkstr(t1,"password")) then
			sdcms.ajaxjson "&#23494;&#30721;&#19981;&#31526;&#21512;&#35201;&#27714;",0
			exit sub
		end if
		data=sdcms.db.dbload(1,"id,islock,logintimes","sd_user","username='"&t0&"' and password='"&md5(t1)&"'","")
		if ubound(data)<0 then
			sdcms.ajaxjson "&#36134;&#25143;&#25110;&#23494;&#30721;&#38169;&#35823;&#65292;&#26080;&#27861;&#32465;&#23450;",0
		else
			select case data(1,0)
				case "-1":sdcms.ajaxjson "&#36134;&#25143;&#34987;&#38145;&#23450;&#65292;&#26080;&#27861;&#32465;&#23450;",0:exit sub
				case "0":sdcms.ajaxjson "&#36134;&#25143;&#26410;&#36890;&#36807;&#37038;&#31665;&#39564;&#35777;&#65292;&#26080;&#27861;&#32465;&#23450;",0:exit sub
			end select
			sdcms.db.dbupdate "sd_user","id="&data(0,0)&"",array(array("logintimes",data(2,0)+1,0,0),array("lastlogindate",now(),50,1),array("lastloginip",sdcms.getip,50,1))
			sdcms.db.insert "sd_user_bind",array(array("openid",alipay_user_id,255,1),array("opentype","alipay",50,1),array("userid",data(0,0),0,0))
			sdcms.resetuserinfo "u.id="&data(0,0)&""
			sdcms.setsession "alipay_user_id",""
			sdcms.setsession "api_backurl",""
			sdcms.ajaxjson "&#32465;&#23450;&#25104;&#21151;",1
		end if
	end sub
	
	sub checkdb()
		if check_user_info then
			sdcms.echo "0&#20250;&#35805;&#36807;&#26399;&#65292;&#26080;&#27861;&#32465;&#23450;"
			exit sub
		end if
		if not(sdcms.getsys("isreg")) then
			sdcms.echo "0&#31995;&#32479;&#26410;&#24320;&#21551;&#20250;&#21592;&#27880;&#20876;&#21151;&#33021;"
			exit sub
		end if
		dim t0,t1,t1_1,t2,t3,t4,data
		t0="alipay"&left(sdcms.getrndfilename,16)
		t1=sdcms.enhtml(sdcms.fpost("password",0))
		t1_1=sdcms.enhtml(sdcms.fpost("repass",0))
		t2=sdcms.enhtml(sdcms.fpost("email",0))
		t3=sdcms.enhtml(sdcms.fpost("imgcode",0))
		t4=sdcms.enhtml(sdcms.fpost("nickname",0))
	
		if not(sdcms.checkpost) then
			sdcms.ajaxjson "禁止外部提交数据",0
			exit sub
		end if

		if sdcms.strlen(t1)=0 then
			sdcms.ajaxjson "密码不能为空",0
			exit sub
		end if
		if not(sdcms.checkstr(t1,"password")) then
			sdcms.ajaxjson "密码由6-16位数字、字母组成",0
			exit sub
		end if
		if sdcms.strlen(t1_1)=0 then
			sdcms.ajaxjson "确认密码不能为空",0
			exit sub
		end if
		if t1<>t1_1 then
			sdcms.ajaxjson "确认密码不正确",0
			exit sub
		end if
		if sdcms.strlen(t4)=0 then
			sdcms.ajaxjson "昵称不能为空",0
			exit sub
		end if
		if sdcms.strlen(t2)=0 then
			sdcms.ajaxjson "电子邮箱不能为空",0
			exit sub
		end if
		if not(sdcms.checkstr(t2,"email")) then
			sdcms.ajaxjson "邮箱格式不正确",0
			exit sub
		end if
		data=sdcms.db.dbcount("sd_user","email='"&t2&"'")
		if data>0 then
			sdcms.ajaxjson "邮箱已存在，请换个试试",0
			exit sub
		end if
		if sdcms.strlen(t3)=0 then
			sdcms.ajaxjson "验证码不能为空",0
			exit sub
		end if
		if sdcms.loadsession("imgcode")<>t3 then
			sdcms.ajaxjson "验证码错误",0
			exit sub
		end if
		dim groupid:groupid=sdcms.getsys("groupid")
		dim useract:useract=sdcms.getsys("ispass")
		dim somecode:somecode=sdcms.getrnd(10)
		
		data=array(array("username",t0,20,1),array("password",md5(t1),16,1),array("nickname",t4,10,1),array("email",t2,50,1),array("groupid",groupid,0,0),array("islock",useract,2,0),array("createdate",sqltime,0,0),array("createip",sdcms.getip,50,1),array("logintimes",1,1,0),array("somecode",somecode,50,1),array("percent",23,2,0),array("amount",0,2,0),array("point",0,2,0),array("isvip",0,2,0),array("avatar",0,0,0))
		sdcms.db.insert "sd_user",data
		
		dim userid:userid=sdcms.db.insertid("id","sd_user")
		sdcms.db.insert "sd_user_bind",array(array("openid",alipay_user_id,255,1),array("opentype","alipay",50,1),array("userid",userid,0,0))
		select case useract
			case "0"
				dim subject,body
				subject=sdcms.getsys("regtitle")
				body=sdcms.dehtml(sdcms.getsys("regcontent"))
				body=replace(body,"{webname}",sdcms.getsys("webname"))
				body=replace(body,"{url}","{weburl}/user/reg.asp?act=reg&uid="&left(t0,20)&"&code="&somecode&"")
				body=replace(body,"{weburl}",weburl)
				sdcms.sendmail t2,subject,body
				sdcms.ajaxjson "1&#36134;&#25143;&#27880;&#20876;&#25104;&#21151;&#65292;&#35831;&#26597;&#25910;&#37038;&#31665;&#40;"&t2&"&#41;&#65292;&#24182;&#28608;&#27963;&#24744;&#30340;&#36134;&#25143;&#65281;",1
			case "-1"
				sdcms.ajaxjson "2&#36134;&#25143;&#27880;&#20876;&#25104;&#21151;&#65292;&#38656;&#35201;&#31649;&#29702;&#21592;&#23457;&#26680;&#21518;&#25165;&#33021;&#30331;&#24405;&#65281;",1
			case "1"
				sdcms.resetuserinfo("u.username='"&t0&"'")
				sdcms.userreg_auto userid
				sdcms.ajaxjson "3&#36134;&#25143;&#27880;&#20876;&#25104;&#21151;&#65281;",1
		end select
		if sdcms.resetuserinfo("u.id="&userid&"") then
			sdcms.setsession "alipay_user_id",""
			sdcms.setsession "api_backurl",""
		else
			sdcms.ajaxjson "&#36134;&#25143;&#32465;&#23450;&#22833;&#36133;",0
		end if
	end sub
	dim act:act=lcase(sdcms.fget("act",0))
	select case act
		case "bind":checkbind()
		case "db":checkdb()
		case else
			if sdcms.is_login then
			dim userdata:userdata=sdcms.userinfo
				dim userid:userid=userdata(0)
				if sdcms.db.dbcount("sd_user_bind","userid="&userid&" and opentype='alipay'")=0 then
					sdcms.db.insert "sd_user_bind",array(array("openid",alipay_user_id,255,1),array("opentype","alipay",50,1),array("userid",userid,0,0))
				end if
				sdcms.setsession "alipay_user_id",""
				sdcms.setsession "api_backurl",""
				sdcms.go api_backurl
			else
				sdcms.show theme_userapibind,""
			end if
	end select
%>